from decimal import *
from datetime import date
from django.db import models
from django.db.models import Q
from django.utils.translation import ugettext_lazy as _
from django.core.urlresolvers import reverse
from django.db.models import permalink
from django.contrib.auth.models import User
from django.db.models.signals import pre_save, post_save

from merchant.apps.coupons import signals
from merchant.apps.business.models import Business
from merchant.apps.categories.models import Category



class CouponManager(models.Manager):
	def get_query_set(self):
		return super(CouponManager, self).get_query_set().filter(
			Q(expiry_date__gt=date.today())|Q(expiry_date=None),
			start_date__lte=date.today()
		)


class Coupon(models.Model):
	# Google Coupon Feeds spec fields.
	business			= models.ForeignKey(Business)
	categories			= models.ManyToManyField(Category)
	title				= models.CharField(_('title'), max_length=25)
	subtitle			= models.CharField(_('subtitle'), max_length=35, blank=True)
	details				= models.TextField(_('details'), max_length=350, blank=True)
	expiry_date 		= models.DateField(_('expiry date'), null=True, blank=True)
	image				= models.ImageField(upload_to='coupons/%Y/%m/%d', blank=True)
	merchant_offer_code	= models.CharField(_('merchant offer code'), max_length=30, blank=True)
	
	# Merchant specific fields.
	slug				= models.SlugField(_('slug'), unique=True)
	start_date			= models.DateField(_('start date'))
	featured			= models.BooleanField(_('featured'), default=False)
	dollar_value		= models.DecimalField(_('dollar value'), max_digits=9, decimal_places=2, null=True, blank=True)
	coupon_url			= models.URLField(_('coupon url'), blank=True, verify_exists=False)
	created				= models.DateTimeField(_('created'), auto_now_add=True)
	modified			= models.DateTimeField(_('modified'), auto_now=True)
	
	objects = models.Manager()
	active_objects = CouponManager()
    
	class Meta:
		verbose_name = _('coupon')
		verbose_name_plural = _('coupons')
		db_table = 'merchant_coupon'
		ordering = ['title']
		get_latest_by = 'created'

	def __unicode__(self):
		return u'%s' % self.title
	
	def get_absolute_url(self):
		return reverse('merchant_coupon_detail', args=[self.slug])
	
	def coupon_code(self):
		return ("%s%s-%s" % (self.business.name[:3], self.created.strftime('%y%m%d'), self.id)).upper()
	coupon_code.short_description = 'coupon code'


class Organizer(models.Model):
	user				= models.ForeignKey(User, unique=True)
	coupons				= models.ManyToManyField(Coupon)
	created				= models.DateTimeField(_('created'), auto_now_add=True)
	modified			= models.DateTimeField(_('modified'), auto_now=True)
	
	class Meta:
		verbose_name = _('organizer')
		verbose_name_plural = _('organizers')
		db_table = 'merchant_organizer'
		ordering = ['user']
		get_latest_by = 'created'
	
	def __unicode__(self):
		return u'%s' % self.user
	
	def total_organizer_value(self):
		total_dollar_value = 0
		for coupon in self.coupons.all():
			if coupon.dollar_value != None:
				total_dollar_value = coupon.dollar_value + total_dollar_value
		return '%s' % Decimal(total_dollar_value)
	total_organizer_value.short_description = 'total value'


#pre_save.connect(signals.pre_object_save, sender=Organizer)
#post_save.connect(signals.post_object_save, sender=Organizer)